/*
 * version 4 by Bush:
 * 		1. add areSectionsConflicted()
 * 
 * version 3 by Bush: 
 * 		1. add copy constructor
 * 		2. add getEndTime
 * 
 * version 2 by Bush: 
 * 		1. change _day from String to int
 * 		2. add toInt()
 * 
 * version 1 by Bush:
 * 		1. change name to SimpleTime
 * 
 */

package smallTools;

public final class SimpleTime {
	private int _day;
	private int _hour;
	private int _minute;
	
	public SimpleTime()
	{
		_day=0;
		_hour=0;
		_minute=0;
	}
	public SimpleTime(String day,int hour,int minute)
	{
		_day=dayStringToInt(day);
		_hour=hour%24;
		_minute=minute%60;
	}
	
	public SimpleTime(SimpleTime rhs)
	{
		_day=rhs._day;
		_hour=rhs._hour;
		_minute=rhs._minute;
	}
	
	public int toInt()
	{
		return _day*10000+_hour*100+_minute;
	}
	
	public String toString()
	{
		String strHour=Integer.toString(_hour);
		if(strHour.length() == 1)
			strHour = "0"+strHour;
		
		String strMinute=Integer.toString(_minute);
		if(strMinute.length() == 1)
			strMinute = "0"+strMinute;
		
		return dayIntToString(_day)+" "+strHour+":"+strMinute;
	}
	
	public static int dayStringToInt(String day)
	{
		if(day == "Sun")
			return 0;
		else if(day == "Mon")
			return 1;
		else if(day == "Tue")
			return 2;
		else if(day == "Wed")
			return 3;
		else if(day == "Thu")
			return 4;
		else if(day == "Fri")
			return 5;
		else if(day == "Sat")
			return 6;
		else
			return 0;
	}
	
	public static String dayIntToString(int day)
	{
		switch(day)
		{
			case 0: return "Sun";
			case 1: return "Mon";
			case 2: return "Tue";
			case 3: return "Wed";
			case 4: return "Thu";
			case 5: return "Fri";
			case 6: return "Sat";
			default: return "Sun";
		}
	}
	
	public SimpleTime getEndTime(int duration)
	{
		SimpleTime result = new SimpleTime(this);
		result.addHour(duration);
		return result;
	}
	
	public void addHour(int hour)
	{
		_hour+=hour;
		if(_hour >= 24)
		{
			_hour = _hour%24;
			_day+=1;
			if(_day == 7)
				_day=0;
		}
	}

	public int getDay()
	{
		return _day;
	}

	public int getHour()
	{
		return _hour;
	}

	public int getMinute()
	{
		return _minute;
	}
	
	public static boolean areSectionsConflicted(SimpleTime startTime1,int duration1,SimpleTime startTime2,int duration2)
	{
		SimpleTime endTime1=startTime1.getEndTime(duration1);
		SimpleTime endTime2=startTime2.getEndTime(duration2);
		
		if(endTime1.toInt() <= startTime2.toInt())
			return false;
		if(endTime2.toInt() <= startTime1.toInt())
			return false;
		
		return true;
	}
}
